﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;

public partial class Delivery_DeliveryApply : INTPage
{
    SqlHelp help = new SqlHelp();
    CommonMethod comHelp = new CommonMethod();
    DeliveryModel model = new DeliveryModel();
    DeliveryDAO comm = new DeliveryDAO();
    DeliveryBuild build = new DeliveryBuild();

    #region
    private string _tradingTable;
    public string tradingTable
    {
        get { return _tradingTable; }
        set { _tradingTable = value; }
    }
    public string getTrading()
    {
        return tradingTable;
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl1);
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
        if (!IsPostBack)
        {
            // 页面权限
            comHelp.GetRight(this, new string[] { Session["UserCode"].ToString(), 
                Session["CoCode"].ToString(), "DeliveryApply" }, null,
                new string[] { "cmdSubmit", "cmdSubmit" }, new int[] { }, CommonModel.loginOut1);
            //cmdSubmit.Visible = false;
            txtUserCode.Value = Session["UserCode"].ToString();
            txtCoCode.Value = Session["CoCode"].ToString();
            bindInti(Session["UserCode"].ToString());
        }
        //if (Page.Request.QueryString["ContractNoMain"] != null)
        //{CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnload();", this); }
        //else {  }
    }

    private void bindInti(string userCode)
    {
        //ContractNoBind("", "");
        //DataTable[] dt = comm.GetSalesNameCustomerName();
        //ddlSaleName.DataSource = dt[0];
        //ddlSaleName.DataValueField = "SalesName";
        //ddlSaleName.DataTextField = "SalesName";
        //ddlSaleName.DataBind();
        //ddlSaleName.Items.Insert(0, new ListItem("请选择(Selelct Please)", "-1"));
        //ddlCustomer.DataSource = dt[1];
        //ddlCustomer.DataValueField = "CustomerName";
        //ddlCustomer.DataTextField = "CustomerName";
        //ddlCustomer.DataBind();
        //ddlCustomer.Items.Insert(0, new ListItem("请选择(Selelct Please)", "-1"));
        
        if (Page.Request.QueryString["ContractNoMain"] != null)
        {
            model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
            model.pageNextIndex = 1;
            model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
            lblPageIndex.Text = model.pageNextIndex.ToString();
            //绑定内容
            string contractN = Page.Request.QueryString["ContractNoMain"].ToString();
            model.ContractNoMain = contractN;
            model.UserCode = userCode;
            model.ActionFlag = false;
            model.applielFlag = 1;
            TradingTableBuild(model);

            txtContractNo.Text = contractN;

            DataTable dt = comm.GetSoNoCustomerSaleName(contractN);
            if (dt.Rows.Count > 0)
            {
                ddlSoNo.DataSource = dt;
                ddlSoNo.DataTextField = "TradingSoNo";
                ddlSoNo.DataValueField = "ContractNoMain";
                ddlSoNo.DataBind();
                txtSoNo.Text = ddlSoNo.SelectedItem.Text;
                string customerName = dt.Rows[0]["CustomerName"].ToString();
                string salesName = dt.Rows[0]["SalesName"].ToString();
                txtCustomer.Text = customerName;
                txtSaleName.Text = salesName;
                txtContractNoCustomer.Text = dt.Rows[0]["ContractNoCust"].ToString();
            }

            //CommonMethod.regAlertMessageScript(UpdHead, "showHidden('imgSoNo','imgSoNo', 'txtSoNo','txtSoNo')", this);
            //CommonMethod.regAlertMessageScript(UpdHead, "showHidden('imgSaleName','imgSaleName', 'txtSoNo','txtSoNo')", this);
            //CommonMethod.regAlertMessageScript(UpdHead, "showHidden('imgCustomer','imgCustomer', 'txtSoNo','txtSoNo')", this);
            //CommonMethod.regAlertMessageScript(UpdHead, "showHidden('imgContractNoCustomer','imgContractNoCustomer', 'txtSoNo','txtSoNo')", this);
            //CommonMethod.regAlertMessageScript(UpdHead, "showHidden('imgContractNo','imgContractNo', 'txtSoNo','txtSoNo')", this);

            imgContractNo.Disabled = true;
            imgContractNoCustomer.Disabled = true;
            imgCustomer.Disabled = true;
            imgSaleName.Disabled = true;
            imgSoNo.Disabled = true;
            cmdSubmit.Visible = false; //cmdQuery.Visible = false;
        }
        else
        {
            DDLBind();
            #region
            if (!ddlContractNo.SelectedValue.Equals("-1"))
            {
                SoNoBind(ddlContractNo.SelectedValue);

                model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
                model.pageNextIndex = 1;
                model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
                lblPageIndex.Text = model.pageNextIndex.ToString();
                //绑定内容
                model.ContractNoMain = ddlContractNo.SelectedValue;
                model.UserCode = userCode;

                TradingTableBuild(model);
            }
            else
            {
                ddlCustomer.SelectedIndex = 0;
                ddlSaleName.SelectedIndex = 0;
            }
            #endregion
        }
    }
    public void TradingTableBuild(DeliveryModel model)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl1);

        lblTradingAllAmount.Text = "合同总金额: 0";
        txtApprovalAmount.Text = "0";
        txtApprovedAmout.Text = "0";
        txtApprovalSelectedAmount.Text = "0";

        model.UserCode = Session["UserCode"].ToString();
        model.CoCode = Session["CoCode"].ToString();
        model.SoNo = "";
        if (!string.IsNullOrEmpty(ddlSoNo.SelectedValue) && !ddlSoNo.SelectedValue.Equals("-1"))
        { model.SoNo = ddlSoNo.SelectedItem.Text; }

        txtApprovalSelectedAmount.Text = "0";
        DataTable[] dt = help.RunProcGetTables("ForTradingHeadBuild",
            new SqlParameter[]{
            new SqlParameter("@ContractNoMain", model.ContractNoMain),
            new SqlParameter("@SoNo", model.SoNo),
            new SqlParameter("@UserCode", model.UserCode),
            new SqlParameter("@appliedFlag", model.applielFlag)
            });
        StringBuilder table = new StringBuilder();
        if (dt.Length > 2 && dt[0].Rows.Count > 0 && dt[1].Rows.Count > 0 && dt[2].Rows.Count > 0)
        {
            table.Append("<table class=\"deliveryApplytTable\" id=\"contractTB\"  border=\"1px\" cellpadding=\"0px\" cellspacing=\"0px\"><thead>");
            table.Append(build.TradingHeadBuild(dt[0]));
            table.Append("</thead>");
            table.Append(build.TradingRowBuild(dt, model));
            table.Append("</table>");

            SetPage(dt[1], model);
            lblTradingAllAmount.Text = "合同总金额: " + dt[2].Rows[0]["TradingAmount"].ToString();
            txtApprovalAmount.Text = dt[2].Rows[0]["TradingApprovalAmount"].ToString();
            txtApprovedAmout.Text = dt[2].Rows[0]["TradingApprovedAmount"].ToString();
            txtApprovalSelectedAmount.Text = dt[2].Rows[0]["TradingApprovingAmount"].ToString();
        }
        else { table.Append("Can not get data！"); }
        Session["TradingApply"] = table.ToString();

        tradingTable = table.ToString();
        CommonMethod.regAlertMessageScript(UpdHead, "GetSelectedAmount();", this);
    }

    #region 

    public void ContractNoBind(string strCondiction, string morceCondiction)
    {
        DataTable dt = comm.GetContractNo(strCondiction, morceCondiction);
        
        if (dt.Rows.Count > 0)
        {
            ddlContractNo.DataSource = dt;
            ddlContractNo.DataTextField = "ContractNoMain";
            ddlContractNo.DataValueField = "ContractNoMain";
            ddlContractNo.DataBind();
            txtContractNo.Text = ddlContractNo.SelectedValue;
            if (dt.Rows.Count < 2)
            { CommonMethod.regAlertMessageScript(UpdHead, "showOrHidden('imgContractNo','none')", this); }            
        }
        else { ddlContractNo.Items.Clear(); ddlContractNo.Items.Insert(0, new ListItem("请选择(Selelct Please)", "-1")); }
        //SetFlag(true);
    }
    public void SoNoBind(string contractNo)
    {
        DataTable dt = comm.GetSoNoCustomerSaleName(contractNo);
        if (dt.Rows.Count > 0)
        {
            ddlSoNo.DataSource = dt;
            ddlSoNo.DataTextField = "TradingSoNo";
            ddlSoNo.DataValueField = "TradingSoNo";
            ddlSoNo.DataBind();
            txtSoNo.Text = ddlSoNo.SelectedItem.Text;
            string customerName = dt.Rows[0]["CustomerName"].ToString();
            string salesName = dt.Rows[0]["SalesName"].ToString();
            string contractNoCust = dt.Rows[0]["ContractNoCust"].ToString();
            txtCustomer.Text = customerName;
            txtSaleName.Text = salesName;
            txtContractNoCustomer.Text = contractNoCust;

            int sn = ddlSaleName.Items.Count, k = 0;
            int cn = ddlCustomer.Items.Count, m = 0;
            int cc = ddlContractNoCustomer.Items.Count, c = 0;
            if (!string.IsNullOrEmpty(salesName))
            {
                for (int i = 0; i < sn; i++)
                {
                    if (ddlSaleName.Items[i].Text.Equals(salesName))
                    { k = i; break; }
                }
            }
            if (!string.IsNullOrEmpty(customerName))
            {
                for (int j = 0; j < cn; j++)
                {
                    if (ddlCustomer.Items[j].Text.Equals(customerName))
                    { m = j; break; }
                }
            }
            if (!string.IsNullOrEmpty(contractNoCust))
            {
                for (int g = 0; g < cc; g++)
                {
                    if (ddlContractNoCustomer.Items[g].Text.Equals(contractNoCust))
                    { c = g; break; }
                }
            }

            if (k > 0)
            { ddlSaleName.SelectedIndex = k; }
            else { ddlSaleName.SelectedIndex = 0; }

            if (m > 0)
            { ddlCustomer.SelectedIndex = m; }
            else { ddlCustomer.SelectedIndex = 0; }

            if (c > 0) { ddlContractNoCustomer.SelectedIndex = c; }
            else { ddlContractNoCustomer.SelectedIndex = 0; }

            if (dt.Rows.Count < 2)
            { CommonMethod.regAlertMessageScript(UpdHead, "showOrHidden('imgSoNo','none')", this); }
        }
        else
        {
            txtSoNo.Text = "";
            txtSaleName.Text = "";
            txtCustomer.Text = "";
            ddlSoNo.Items.Clear();
            ddlCustomer.SelectedIndex = 0;
            ddlSaleName.SelectedIndex = 0;
            ddlContractNoCustomer.SelectedIndex = 0;
        }
    }
    public void SetFlag(bool flag)
    {
        bool flag2 = !flag;
        txtContractNo.Visible = flag;
        ddlContractNo.Visible = flag2;
        txtSoNo.Visible = flag;
        ddlSoNo.Visible = flag2;
        txtCustomer.Visible = flag;
        ddlCustomer.Visible = flag2;
        txtSaleName.Visible = flag;
        ddlSaleName.Visible = flag2;
    }

    #endregion   

    // 换页设置
    public void SetPage(DataTable dt, DeliveryModel model)
    {
        int toaltNum = dt.Rows.Count;
        int pagesize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        this.lblTotal.Text = toaltNum.ToString();
        int pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        int pageCount = toaltNum / pagesize;

        if (pageCount * pagesize == toaltNum) { }
        else { pageCount = pageCount + 1; }

        txtNewPageIndex.Value = pageIndex.ToString();

        this.lblPageCount.Text = pageCount.ToString();

        if (pageIndex < pageCount)
        {
            if (pageIndex == 1)
            {
                lbtFirstPage.Enabled = false;
                lbtPreviousPage.Enabled = false;
                lbtNextPage.Enabled = true;
                lbtLastPage.Enabled = true;
            }
            else
            {
                lbtFirstPage.Enabled = true;
                lbtPreviousPage.Enabled = true;
                lbtNextPage.Enabled = true;
                lbtLastPage.Enabled = true;
            }
        }
        else
        {
            if (pageIndex > 1)
            {
                lbtFirstPage.Enabled = true;
                lbtPreviousPage.Enabled = true;
                lbtNextPage.Enabled = false;
                lbtLastPage.Enabled = false;
                btnGo.Enabled = true;
            }
            else
            {
                lbtFirstPage.Enabled = false;
                lbtPreviousPage.Enabled = false;
                lbtNextPage.Enabled = false;
                lbtLastPage.Enabled = false;
                btnGo.Enabled = false;
            }
        }
    }

    #region 换页
    // 跳页
    protected void btnGo_Click(object sender, EventArgs e)
    {
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = Convert.ToInt16(txtNewPageIndex.Value.ToString());
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        if (!(model.pageIndex == model.pageNextIndex))
        {
            model.ContractNoMain = ddlContractNo.SelectedValue;
            TradingTableBuild(model);
        }
    }
    //第一页
    protected void lbtFirstPage_Click(object sender, EventArgs e)
    {
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = 1;
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.ContractNoMain = ddlContractNo.SelectedValue;
        TradingTableBuild(model);
    }
    // 上一页
    protected void lbtPreviousPage_Click(object sender, EventArgs e)
    {
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = Convert.ToInt16(lblPageIndex.Text.ToString()) - 1;
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.ContractNoMain = ddlContractNo.SelectedValue;
        TradingTableBuild(model);
    }
    // 下一页
    protected void lbtNextPage_Click(object sender, EventArgs e)
    {
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = Convert.ToInt16(lblPageIndex.Text.ToString()) + 1;
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.ContractNoMain = ddlContractNo.SelectedValue;
        TradingTableBuild(model);
    }
    // 最后一页
    protected void lbtLastPage_Click(object sender, EventArgs e)
    {
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = Convert.ToInt16(lblPageCount.Text.ToString());
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.ContractNoMain = ddlContractNo.SelectedValue;
        TradingTableBuild(model);
    }
    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl1);
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = 1;
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.ContractNoMain = ddlContractNo.SelectedValue;
        model.UserCode = Session["UserCode"].ToString();
        TradingTableBuild(model);
    }
    #endregion

    public void DDLBind()
    {
        ListItem item = new ListItem("请选择(Select Please)", "-1");
        DataTable[] dt = comm.GetddlForApply();
        if (dt != null && dt.Length > 3)
        {
            ddlContractNo.DataSource = dt[0];
            ddlContractNo.DataValueField = "ContractNoMain";
            ddlContractNo.DataTextField = "ContractNoMain";
            ddlContractNo.DataBind();

            ddlContractNoCustomer.DataSource = dt[6];
            ddlContractNoCustomer.DataValueField = "ContractNoMain";
            ddlContractNoCustomer.DataTextField = "ContractNoCust";
            ddlContractNoCustomer.DataBind();

            ddlSoNo.DataSource = dt[1];
            ddlSoNo.DataValueField = "ContractNoMain";
            ddlSoNo.DataTextField = "TradingSoNo";
            ddlSoNo.DataBind();

            ddlSaleName.DataSource = dt[2];
            ddlSaleName.DataValueField = "SalesName";
            ddlSaleName.DataTextField = "SalesName";
            ddlSaleName.DataBind();

            ddlCustomer.DataSource = dt[3];
            ddlCustomer.DataValueField = "CustomerName";
            ddlCustomer.DataTextField = "CustomerName";
            ddlCustomer.DataBind();
        }
        ddlContractNo.Items.Insert(0, item);
        ddlContractNoCustomer.Items.Insert(0, item);
        ddlSoNo.Items.Insert(0, item);
        ddlSaleName.Items.Insert(0, item);
        ddlCustomer.Items.Insert(0, item);

        ddlContractNo.SelectedIndex = 0;
        ddlContractNoCustomer.SelectedIndex = 0;
        ddlSoNo.SelectedIndex = 0;
        ddlSaleName.SelectedIndex = 0;
        ddlCustomer.SelectedIndex = 0;
        txtContractNo.Text = "";
        txtContractNoCustomer.Text = "";
        txtSoNo.Text = "";
        txtSaleName.Text = "";
        txtCustomer.Text = "";
    }
    protected void ddlContractNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
        string contractNo = ddlContractNo.SelectedValue.ToString();
        if (!contractNo.Equals("-1"))
        {
            SoNoBind(contractNo);

            model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
            model.pageNextIndex = 1;
            model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
            lblPageIndex.Text = model.pageNextIndex.ToString();
            //绑定内容
            model.ContractNoMain = contractNo;
            model.UserCode = Session["UserCode"].ToString();
            TradingTableBuild(model);

            txtContractNo.Text = contractNo;
        }
        else
        {
            tradingTable = "Not Data For the Contract!";
            ddlCustomer.SelectedIndex = 0;
            txtCustomer.Text = "";
            ddlSaleName.SelectedIndex = 0;
            txtSaleName.Text = "";
            ddlSoNo.Items.Clear();
            txtContractNoCustomer.Text = "";
            ddlContractNoCustomer.SelectedIndex = 0;
        }

        CommonMethod.regAlertMessageScript(UpdHead, "showHidden('ddlContractNo','ddlContractNo', 'txtContractNo','txtContractNo')", this);
    }
    protected void ddlSoNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        int soN = ddlSoNo.SelectedIndex;
        string strContractNo = string.Format(" and T.TradingSoNo='{0}' and T.DeleteFlag=0 ", ddlSoNo.SelectedItem.Text);
        ContractNoBind(strContractNo, "");
        if (!ddlContractNo.SelectedValue.Equals("-1"))
        {
            SoNoBind(ddlContractNo.SelectedValue);
            if (soN < ddlSoNo.Items.Count)
            { ddlSoNo.SelectedIndex = soN; }
            txtSoNo.Text = ddlSoNo.SelectedItem.Text;

            model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
            model.pageNextIndex = 1;
            model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
            lblPageIndex.Text = model.pageNextIndex.ToString();
            //绑定内容
            model.ContractNoMain = ddlContractNo.SelectedValue;
            model.UserCode = Session["UserCode"].ToString();

            TradingTableBuild(model);
        }
        else
        {
            tradingTable = "Not Data For the Contract!";
            //ddlCustomer.SelectedIndex = 0;
            //txtCustomer.Text = "";
            //txtContractNoCustomer.Text = "";
            //ddlContractNoCustomer.SelectedIndex = 0;
            //txtSoNo.Text = "";
            DDLBind();
        }
        //if (Session["TradingApply"] != null) { tradingTable = Session["TradingApply"].ToString(); }               

        CommonMethod.regAlertMessageScript(UpdHead, "showHidden('ddlSoNo','ddlSoNo', 'txtSoNo','txtSoNo')", this);
    }
    protected void ddlCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtCustomer.Text = ddlCustomer.SelectedItem.Text;
        string strtemp = "  join M_Customer C on T.ContractNoMain=C.ContractNoMain and C.CustomerName='{0}' and C.DeleteFlag=0 and T.DeleteFlag=0 ";
        string strOthers = string.Format(strtemp, txtCustomer.Text);

        ContractNoBind("", strOthers);
        if (!ddlContractNo.SelectedValue.Equals("-1"))
        {
            SoNoBind(ddlContractNo.SelectedValue);

            model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
            model.pageNextIndex = 1;
            model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
            lblPageIndex.Text = model.pageNextIndex.ToString();
            //绑定内容
            model.ContractNoMain = ddlContractNo.SelectedValue;
            model.UserCode = Session["UserCode"].ToString();
            TradingTableBuild(model);
        }
        else
        {
            tradingTable = "Not Data For the Contract!";
            //ddlSoNo.Items.Clear();
            //txtSoNo.Text = "";
            //ddlSaleName.SelectedIndex = 0;
            //txtSaleName.Text = "";
            //ddlContractNoCustomer.SelectedIndex = 0;
            //txtContractNoCustomer.Text = "";
            //txtContractNo.Text = "";
            //ddlContractNo.SelectedIndex = 0;
            DDLBind();
        }

        CommonMethod.regAlertMessageScript(UpdHead, "showHidden('ddlCustomer','ddlCustomer', 'txtCustomer','txtCustomer')", this);
    }
    protected void ddlSaleName_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtSaleName.Text = ddlSaleName.SelectedItem.Text;

        string strOthers = string.Format(" join M_SalesCatalog SC on T.ContractNoMain=SC.ContractNoMain and SC.SalesName='{0}' and C.DeleteFlag=0 and T.DeleteFlag=0 ", txtSaleName.Text);

        ContractNoBind("", strOthers);
        if (!ddlContractNo.SelectedValue.Equals("-1"))
        {
            SoNoBind(ddlContractNo.SelectedValue);

            model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
            model.pageNextIndex = 1;
            model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
            lblPageIndex.Text = model.pageNextIndex.ToString();
            //绑定内容
            model.ContractNoMain = ddlContractNo.SelectedValue;
            model.UserCode = Session["UserCode"].ToString();

            TradingTableBuild(model);
        }
        else
        {
            tradingTable = "Not Data For the Contract!";
            //ddlSoNo.Items.Clear();
            //txtSoNo.Text = "";
            //ddlCustomer.SelectedIndex = 0;
            //txtCustomer.Text = "";
            //ddlContractNoCustomer.SelectedIndex = 0;
            //txtContractNoCustomer.Text = "";
            //txtContractNo.Text = "";
            //ddlContractNo.SelectedIndex = 0;
            DDLBind();
        }

        CommonMethod.regAlertMessageScript(UpdHead, "showHidden('ddlSaleName','ddlSaleName', 'txtSaleName','txtSaleName')", this);
    }
    protected void ddlContractNoCustomer_TextChanged(object sender, EventArgs e)
    {
        if (!ddlContractNoCustomer.SelectedValue.Equals("-1"))
        {
            string contractNo = ddlContractNoCustomer.SelectedValue;
            SoNoBind(contractNo);

            model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
            model.pageNextIndex = 1;
            model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
            lblPageIndex.Text = model.pageNextIndex.ToString();
            //绑定内容
            model.ContractNoMain = contractNo;
            model.UserCode = Session["UserCode"].ToString();
            TradingTableBuild(model);
            try
            { ddlContractNo.SelectedValue = contractNo; }
            catch
            {
                ddlContractNo.Items.Add(new ListItem(contractNo, contractNo));
                ddlContractNo.Items.Add(new ListItem("请选择(Select Please)", "-1"));
            }
            txtContractNo.Text = contractNo;
            txtContractNoCustomer.Text = ddlContractNoCustomer.SelectedItem.Text;
        }
        else
        {
            txtContractNo.Text = "";
            ddlContractNo.SelectedIndex = 0;
            txtSaleName.Text = "";
            ddlSaleName.SelectedIndex = 0;
            txtCustomer.Text = "";
            ddlCustomer.SelectedIndex = 0;
            ddlSoNo.Items.Clear();
            txtSoNo.Text = "";
            //if (Session["TradingApply"] != null) 
            //{ tradingTable = Session["TradingApply"].ToString(); }
            txtContractNoCustomer.Text = "";
        }
        CommonMethod.regAlertMessageScript(UpdHead, "showHidden('ddlSaleName','ddlSaleName', 'txtSaleName','txtSaleName')", this);
    }

    protected void cmdQuery_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
        string strContractNo = string.Empty;
        string strOthers = string.Empty;

        if (!string.IsNullOrEmpty(txtSoNo.Text))
        {
            strContractNo += string.Format(" and T.TradingSoNo like '%{0}%' ", txtSoNo.Text);
        }
        if (!string.IsNullOrEmpty(txtCustomer.Text))
        {
            strOthers += string.Format(" join M_Customer C on T.ContractNoMain=C.ContractNoMain and C.CustomerName like '%{0}%' and C.DeteleFlag=0 ", txtCustomer.Text);
        }
        if(!string.IsNullOrEmpty(txtSaleName.Text))
        {
            strOthers += string.Format(" join M_SalesCatalog SC on T.ContractNoMain=SC.ContractNoMain and SC.SalesName like '%{0}%' and SC.DeteleFlag=0 ", txtSaleName.Text);
        }
        if (!string.IsNullOrEmpty(txtContractNo.Text))
        {
            strContractNo += string.Format(" and T.ContractNoMain like '%{0}%' ", txtContractNo.Text);
        }
        if (!string.IsNullOrEmpty(txtContractNoCustomer.Text))
        {
            strContractNo += string.Format(" and T.ContractNoCust like '%{0}%' ", txtContractNoCustomer.Text);
        }

        ContractNoBind(strContractNo, strOthers);
        if (ddlContractNo.Items.Count > 0)
        {
            //int soN = ddlSoNo.SelectedIndex;
            string soNo = txtSoNo.Text.Trim();
            //if (string.IsNullOrEmpty(txtSoNo.Text.Trim()) && !string.IsNullOrEmpty(ddlSoNo.SelectedValue) && !ddlSoNo.SelectedValue.Equals("-1"))
            //{ soNo = ddlSoNo.SelectedItem.Text; }

            SoNoBind(ddlContractNo.SelectedValue);

            //if (soN < ddlSoNo.Items.Count)
            //{ ddlSoNo.SelectedIndex = soN; }

            if (!string.IsNullOrEmpty(soNo))
            {
                try 
                {
                    for (int i = 0; i < ddlSoNo.Items.Count; i++)
                    {
                        if (ddlSoNo.Items[i].Value.Contains(soNo))
                        {
                            ddlSoNo.SelectedIndex = i;
                            txtSoNo.Text = ddlSoNo.SelectedItem.Text;
                            break;
                        }
                    }
                }
                catch { }
            }

            model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
            model.pageNextIndex = 1;
            model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
            lblPageIndex.Text = model.pageNextIndex.ToString();
            //绑定内容
            model.ContractNoMain = ddlContractNo.SelectedValue;
            model.UserCode = Session["UserCode"].ToString();

            TradingTableBuild(model);            
        }
        else { tradingTable = "Not Data For the Contract!"; }

        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
        //CommonMethod.regAlertMessageScript(UpdHead, "", this);
    }        
}